import re

import requests
import execjs

class Cookie(object):

    def __init__(self):
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36',
        }
        self.url = 'http://sd.gsxt.gov.cn/corp-query-entprise-info-xxgg-370000.html'

    def fetchJS(self):
        scriptRep = requests.get(self.url, headers=self.headers)
        scriptRep.encoding = 'utf-8'
        jsluidh = scriptRep.headers['Set-Cookie'].split(';')[0]
        self.headers['Cookie'] = jsluidh
        return scriptRep

    def calculateJS(self, scriptRep):
        jsCode = re.findall(r'<script>(.*?)</script>', scriptRep.text)
        if jsCode:
            js = jsCode[0]
            js = re.sub('document.*?=', 'return ', js)
            js = js.replace("go({", 'data = go({')
            window_cookie = 'navigator={appCodeName:"Mozilla",appMinorVersion:"0",appName:"Netscape",appVersion:"5.0 (Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; InfoPath.3; rv:11.0) like Gecko",browserLanguage:"zh-CN",cookieEnabled:true,cpuClass:"x86",language:"zh-CN",maxTouchPoints:0,msManipulationViewsEnabled:true,msMaxTouchPoints:0,msPointerEnabled:true,onLine:true,platform:"Win32",pointerEnabled:true,product:"Gecko",systemLanguage:"zh-CN",userAgent:"Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; InfoPath.3; rv:11.0) like Gecko",userLanguage:"zh-CN",vendor:"",vendorSub:"",webdriver:false},window=this,window.navigator=navigator;'+ js
            context = execjs.compile(window_cookie)
            cookie = context.eval('data')
            print(cookie)
            clearance = cookie.split(';')[0]
            self.headers['Cookie'] += ';' + clearance
            print(self.headers)

    def testCookie(self):
        url = 'http://sd.gsxt.gov.cn/corp-query-entprise-info-xxgg-370000.html'
        rep = requests.get(url, headers=self.headers)
        print(rep.text)

if __name__ == '__main__':
    ck = Cookie()
    scriptRep = ck.fetchJS()
    ck.calculateJS(scriptRep)
    ck.testCookie()